package com.itheima.exception;

import com.itheima.common.Code;
import com.itheima.common.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: 全局异常捕获类
 * @Version: V1.0
 * 1、拦截所有的异常，给用户提示友好信息
 * 2、记录日志，对系统维护
 */
@RestControllerAdvice
@Component
public class MyExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    public Result BizException(BusinessException ex) {
        return ex.getResult();
    }

    /**
     * 不可预知异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result BizException(Exception ex) {
        // 记录日志 发短信 ...
        System.out.println(ex);
        // 给用户提示友好信息
        return new Result(Code.ERROR, "您的网络有问题，请稍后重试！");
    }

}
